Imports System.Data.SqlClient
Public Class cls_cobrador_

    Implements ICloneable

#Region "Variables ..."
    Private int_idcobrador As Integer = 0
    Private int_idtercero As Integer = 0

    Private obj_tercero As cls_tercero_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_cobrador_) = Nothing
#End Region

    Public Overrides Function ToString() As String
        Return tercero.ToString
    End Function

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idcobrador
        End Get
    End Property
    Property idcobrador() As Integer
        Get
            idcobrador = int_idcobrador
        End Get
        Set(ByVal value As Integer)
            int_idcobrador = value
        End Set
    End Property
    Property idtercero() As Integer
        Get
            idtercero = int_idtercero
        End Get
        Set(ByVal value As Integer)
            int_idtercero = value
        End Set
    End Property
    Property tercero() As cls_tercero_
        Get
            If IsNothing(obj_tercero) Then
                obj_tercero = New cls_tercero_
                If Not obj_tercero.Buscar(idtercero) Then
                    obj_tercero = Nothing
                End If
            End If
            tercero = obj_tercero
        End Get
        Set(ByVal value As cls_tercero_)
            obj_tercero = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_cobrador_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_cobrador_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_cobrador_)
        While obj_Reader.Read()
            Dim Elemento As New cls_cobrador_
            Elemento.int_idcobrador = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcobrador")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
                Elemento.int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idcobrador = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcobrador")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
            int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try

            'Creacion de Tercero----------------------------------------------------
            tercero.tipo = cls_tercero_.enu_tipotercero.Cliente
            If tercero.idtercero = 0 Then
                tercero.Nuevo()
            Else
                tercero.Modificar()
            End If
            idtercero = obj_tercero.idtercero
            '-----------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cobrador__Nuevo"
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de cobrador_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try

            'Modificacion de Tercero------------------------------------------------
            obj_tercero.Modificar()
            '-----------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cobrador__Modificar"
                .Parameters.Add("@idcobrador", SqlDbType.Int).Value = int_idcobrador
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de cobrador_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idcobrador As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cobrador__Eliminar"
                .Parameters.Add("@idcobrador", SqlDbType.Int).Value = idcobrador
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de cobrador_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idcobrador As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cobrador__Buscar"
                .Parameters.Add("@idcobrador", SqlDbType.Int).Value = idcobrador
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de cobrador_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function
    Public Function BuscarXsubCodigo1(ByVal subcodigo1 As String) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            BuscarXsubCodigo1 = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cobrador__BuscarXsubCodigo1"
                .Parameters.Add("@subcodigo1", SqlDbType.NVarChar).Value = subcodigo1
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                BuscarXsubCodigo1 = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al BuscarXsubCodigo1 el registro de cobrador_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function
    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cobrador__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de cobrador_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos(Optional ByVal idregistroestado As Integer = 1) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cobrador__CargarTodos"
                .Parameters.Add("@idregistroestado", SqlDbType.Int).Value = idregistroestado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de cobrador_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_cobrador_Clon As New cls_cobrador_
        obj_cobrador_Clon.int_idcobrador = Me.int_idcobrador
        obj_cobrador_Clon.int_idtercero = Me.int_idtercero
        obj_cobrador_Clon.col_Items = Me.col_Items
        Return obj_cobrador_Clon
    End Function

End Class
